<?php

namespace Admin \ Controller;

use Think \ Controller;

class MallController extends CommonController {

    function citylist() {
        $gc = M('Mall_city');
        //$where['status'] = array('neq',3);
        $list = $gc->where($where)->select();
        $this->assign('list', $list);
        $this->display('city');
    }

    function addcity() {
        $id = $_REQUEST['id'];
        $gc = M('Mall_city');
        $where['id'] = $id;
        $data = $gc->where($where)->find();
        $this->assign('data',$data);
        $this->display('addcity');
    }

    function savecity() {
        $gc = M('Mall_city');
        $id = $_REQUEST['id'];
        if (false === $data = $gc->create()) {
            $this->error($gc->error());
        }
        if (empty($id)) {
            $data['ctime'] = $this->crtTime();
            $state = $gc->add($data);
        } else {
            $state = $gc->where('id=' . $id)->save($data);
           // echo $gc->getLastSql();exit;
        }
        if ($state) {
            $this->success('数据已保存！', "citylist");
        } else {
            $this->error('数据未保存！');
        }
    }

    function deletecity() {
        
    }

    function setconfig() {
        
        $data = M('Mall_config')->where('cityid='.$_REQUEST['cityid'])->find();
        $this->assign('data', $data);
        $this->display('config');
    }

    function saveconfig() {
        $gc = M('Mall_config');
        $imgPath = $this->upload();
        $city = $_REQUEST['cityid'];
        $imgurl = $gc->where('cityid='.$city)->getField('imgurl');
        if (false === $data = $gc->create()) {
            $this->error($gc->error());
        }
        if ($imgPath) {
            $data['imgurl'] = $imgPath;
        } else {
            $data['imgurl'] = $imgurl;
        }
        $data['rule'] = $_REQUEST['rule'];
        $state = $gc->where('cityid='.$city)->save($data);
        if ($state) {
            $this->success('数据已保存！', "citylist");
        } else {
            $this->error('数据未保存！');
        }
    }

    //显示商品列表
    function productlist() {
        $gc = M('Mall_product');
        $where['cityid'] = $_REQUEST['cityid'];
        $where['status'] = array('neq', 3);
        $list = $gc->where($where)->select();
        $this->assign('list', $list);
        $this->display('list');
    }

    function addproduct() {
        $id = $_REQUEST['id'];
        $gc = M('Mall_product');
        $where['id'] = $id;
        $data = $gc->where($where)->find();
        $this->assign('data', $data);
        $this->display('add');
    }

    function doAdd() {
        $gc = M('Mall_product');
        $id = $_REQUEST['id'];
        $imgPath = $this->upload();
        $imgurl = $gc->where('id=' . $id)->getField('imgurl');
        if (false === $data = $gc->create()) {
            $this->error($gc->error());
        }
        if ($imgPath) {
            $data['imgurl'] = $imgPath;
        } else {
            $data['imgurl'] = $imgurl;
        }
        $data['intro'] = $_REQUEST['intro'];
        if (empty($id)) {
            $data['ctime'] = $data['utime'] = $this->crtTime();
            $state = $gc->add($data);
        } else {
            $data['utime'] = $this->crtTime();
            $state = $gc->where('id=' . $id)->save($data);
        }
        if ($state) {
            $this->success('数据已保存！', "productlist");
        } else {
            $this->error('数据未保存！');
        }
    }

    function delete() {
        $gc = M('Mall_product');
        $id = $_REQUEST['id'];
        $data['utime'] = $this->crtTime();
        $data['status'] = 3;
        $state = $gc->where('id=' . $id)->save($data);
        if ($state) {
            $this->success('数据已保存！', "productlist");
        } else {
            $this->error('数据未保存！');
        }
    }

    function order() {
        
        $this->display('order');
    }

}
